package net.osmand.plus.quickaction;


import android.content.Context;
import androidx.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.MapActivity;
import net.osmand.util.Algorithms;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QuickAction {

    public interface QuickActionSelectionListener {

        void onActionSelected(QuickAction action);
    }
    private static int SEQ = 0;

    protected long id;
    private String name;
    private Map<String, String> params;
    private QuickActionType actionType;

    protected QuickAction() {
        this(QuickActionRegistry.TYPE_ADD_ITEMS);
    }

    public QuickAction(QuickActionType type) {
        this.id = System.currentTimeMillis() + (SEQ++);
        this.actionType = type;
    }

    public QuickAction(QuickAction quickAction) {
		this.actionType = quickAction.actionType;
        this.id = quickAction.id;
        this.name = quickAction.name;
        this.params = quickAction.params;
    }

    public int getNameRes() {
    	return actionType == null ? 0 : actionType.getNameRes();
    }

	public int getActionNameRes() {
		return actionType == null ? 0 : actionType.getActionNameRes();
	}

    public int getIconRes() {
		return actionType == null ? 0 : actionType.getIconRes();
    }

    public int getIconRes(Context context) {
		return actionType == null ? 0 : actionType.getIconRes();
    }

    public long getId() {
        return id;
    }

	public int getType() {
		return actionType.getId();
	}

	public void setActionType(QuickActionType actionType) {
		this.actionType = actionType;
	}

    public boolean isActionEditable() {
        return actionType == null ? false : actionType.isActionEditable();
    }

    public boolean isActionEnable(OsmandApplication app) {
        return true;
    }

    public String getName(Context context) {
        if (Algorithms.isEmpty(name)) {
			return getDefaultName(context);
		} else {
            return name;
        }
    }

    public String getRawName() {
    	return name;
	}

	@NonNull
	private String getDefaultName(Context context) {
		return getNameRes() != 0 ? context.getString(getNameRes()) : "";
	}

	public Map<String, String> getParams() {
        if (params == null) {
        	params = new HashMap<>();
		}
        return params;
    }

    public void setName(String name) {
        this.name = name;
    }

	public void setId(long id) {
		this.id = id;
	}

	public void setParams(Map<String, String> params) {
        this.params = params;
    }

    public boolean isActionWithSlash(OsmandApplication application){
        return false;
    }

    public String getActionText(OsmandApplication application){
        return getName(application);
    }

	public QuickActionType getActionType() {
		return actionType;
	}

	public void setAutoGeneratedTitle(EditText title) {
    }

    public void execute(@NonNull MapActivity mapActivity) {
	}

	public void drawUI(@NonNull ViewGroup parent, @NonNull MapActivity mapActivity) {
	}

    public boolean fillParams(@NonNull View root, @NonNull MapActivity mapActivity) {
    	return true;
    }

    public boolean hasInstanceInList(List<QuickAction> active) {

		for (QuickAction action : active) {
			if (action.getType() == getType()) return true;
		}

        return false;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null) return false;

        if (o instanceof QuickAction) {

            QuickAction action = (QuickAction) o;

            if (getType() != action.getType()) return false;
            if (id != action.id) return false;

            return true;

        } else return false;
    }

    @Override
    public int hashCode() {
        int result = getType();
        result = 31 * result + (int) (id ^ (id >>> 32));
        return result;
    }

    public boolean hasCustomName(Context context) {
        return !getName(context).equals(getDefaultName(context));
    }
}
